package com.lrkj.back.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.lrkj.back.service.TbModuleService;
import com.lrkj.back.entity.TbModule;
import com.lrkj.back.Const;
//import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/url")
/**
 * 菜单管理controller 实现对菜单的修改 排序等管理。
 */
public class TbModuleController extends BaseController {

	@Autowired
	private TbModuleService moduleService;


	@RequestMapping(value="/moduleList.do")
	public ModelAndView moduleList(String searchTxt,HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Map<String,Object> map = new HashMap<>();
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		map.put("moduleParent", "all");
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<TbModule> page = moduleService.findAllTbModule(map);
		request.setAttribute("pageIndex",pageNo);
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPageSize());
		return new ModelAndView("/system/caidanguangli/module_list");
	}

	@RequestMapping(value="/moduleByType")
	@ResponseBody
	public Map<String,Object> moduleByType(String searchTxt,HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Map<Integer,String> rtnMap = new HashMap<>();
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<>();
		Map<String,Object> resultMap = new HashMap<>();
		if(StringUtils.isNotBlank(searchTxt)){
			map.put("moduleName", searchTxt.trim());
		}
		map.put("moduleParent", "all");
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<TbModule> page = moduleService.findAllTbModule(map);
		map.remove("moduleName");
		List<TbModule> list = moduleService.findAllTbModules(map);
		List<TbModule> modeleList = new ArrayList<>();
		for(TbModule module:list){
			rtnMap.put(module.getModuleId(), module.getModuleName());
			if(module.getModuleParent()==null){
				modeleList.add(module);
			}
		}
		List<TbModule> modules = page.getList();
		//对象出入后台vue 无发解析。要转换为json字符串
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		resultMap.put("urlList",modules);
		resultMap.put("parentList",modeleList);
		resultMap.put("moduleMap",rtnMap);
		return  resultMap;
	}
	
	@RequestMapping(value="/moduleEdit.do")
	public String moduleEdit(Integer id,HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		TbModule module = (id == null) ? new TbModule() : moduleService.selectByPrimaryKey(id);
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("moduleParent", null);
		List<TbModule> moduleList = moduleService.findAllTbModules(paramMap);
		request.setAttribute("moduleList", moduleList);
		request.setAttribute("module", module);
		return "/system/caidanguangli/module_edit";
	}
	
	@RequestMapping(value="/moduleSave.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<Object,Object> moduleSave(TbModule module,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			module.setModuleCreatetime(new Date());
			if(module.getModuleId() == null ){
				moduleService.insert(module);
			}else{
				moduleService.updateByPrimaryKey(module);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("/moduleDelete.do")
	@ResponseBody
	public Map<Object,Object> moduleDelete(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			for(String tid : arraId){
				moduleService.deleteByPrimaryKey(Integer.valueOf(tid));
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
}
